package com.ac.scum.log.web.model.card.controller;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.Card;
import com.ac.scum.log.bean.Server;
import com.ac.scum.log.web.interceptor.anno.PermissionCheck;
import com.ac.scum.log.web.model.card.service.CardService;
import com.ac.scum.log.web.model.login.service.EncodingSalt;
import com.ac.scum.log.web.model.server.mapper.ServerMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CDK管理
 */
@RestController
@RequestMapping("card")
@AllArgsConstructor
public class CardController {
    private final ServerMapper serverMapper;
    private final CardService cardService;
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final EncodingSalt encodingSalt;


    @PermissionCheck(permission = "cdk")
    @GetMapping("page/{current}/{size}")
    public Result<Page<Card>> getCardListPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return Result.ok(cardService.page(new Page<>(current, size)));
    }

    @PermissionCheck(permission = "cdk")
    @GetMapping("used/by_server/{serverId}/{current}/{size}")
    public Result<Page<Card>> getUsedCardListPage(@PathVariable("serverId") Long serverId, @PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return Result.ok(cardService.page(new Page<>(current, size), Wrappers.lambdaQuery(Card.class).inSql(Card::getId, "select card_id from card_used where server_id = " + serverId + " ")));
    }

    @PermissionCheck(permission = "cdk")
    @GetMapping("used/by_card/{cardId}/{current}/{size}")
    public Result<Page<Server>> getUsedCardServerListPage(@PathVariable("cardId") Long cardId, @PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return Result.ok(serverMapper.selectPage(new Page<>(current, size), Wrappers.lambdaQuery(Server.class).inSql(Server::getId, "select server_id from card_used where card_id = " + cardId + " ")));
    }

    @PutMapping
    @PermissionCheck(permission = "cdk")
    public Result<Card> modify(@RequestBody Card card) {
        cardService.updateById(card);
        return Result.ok(card);
    }

    @PostMapping
    @PermissionCheck(permission = "cdk")
    public Result<Boolean> insert(@RequestParam("packageId") Long packageId, @RequestParam("count") Long count, @RequestParam("hours") Integer hours, @RequestParam("type") Integer type) {
        List<Card> cards = Lists.newArrayList();
        for (int i = 0; i < count; i++) {
            cards.add(new Card() {{
                setType(type);
                setHours(hours);
                setPackageId(packageId);
                setKeyCard(encodingSalt.encoding(String.valueOf(snowflakeIdGenerator.nextId())).getAfter());
            }});
        }
        cardService.saveBatch(cards);
        return Result.ok(Boolean.TRUE);
    }

    @DeleteMapping("{id}")
    @PermissionCheck(permission = "cdk")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(cardService.removeById(id));
    }

}
